<template>
  <div>
    <div id="container"></div>
    <div class="input-card">
      <h4>轨迹回放控制</h4>
      <div class="input-item">
        <input type="button" class="btn" value="开始动画" id="start" @click="startAnimation()" />
        <input type="button" class="btn" value="暂停动画" id="pause" @click="pauseAnimation()" />
      </div>
      <div class="input-item">
        <input type="button" class="btn" value="继续动画" id="resume" @click="resumeAnimation()" />
        <input type="button" class="btn" value="停止动画" id="stop" @click="stopAnimation()" />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
// 请求路径
// import {
// playbacklist,
// } from "@/api/obd/playback";
import AMapLoader from '@amap/amap-jsapi-loader'

import { ref, onMounted, onBeforeUnmount } from 'vue'
// let AMap = ''
onMounted(async () => {
  // window._AMapSecurityConfig = {
  //     securityJsCode: '8e920f73eb2e6880a92ea6662eefc476',
  // }
  // AMap = await AMapLoader.load({
  //     key: "e6cf30fd79d7b556ee881ddd0281e8d0", // 申请好的Web端开发者Key，首次调用 load 时必填
  //     version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
  //     plugins: [
  //     "AMap.Scale", //工具条，控制地图的缩放、平移等
  //     "AMap.ToolBar", //比例尺，显示当前地图中心的比例尺
  //     "AMap.Geolocation", //定位，提供了获取用户当前准确位置、所在城市的方法
  //     "AMap.HawkEye", //鹰眼，显示缩略图
  //     "AMap.MoveAnimation",
  //     "AMap.PolyEditor",
  //     "AMap.MouseTool"
  //     ]
  // })
  initMap()
})
onBeforeUnmount(() => {
  map.value && map.value.destroy()
})
const map = ref()
const marker = ref()
const Circle = ref()
const lineArr = ref([
  [116.478935, 39.997761],
  [116.478939, 39.997825],
  [116.478912, 39.998549],
  [116.478912, 39.998549],
  [116.478998, 39.998555],
  [116.478998, 39.998555],
  [116.479282, 39.99856],
  [116.479658, 39.998528],
  [116.480151, 39.998453],
  [116.480784, 39.998302],
  [116.480784, 39.998302],
  [116.481149, 39.998184],
  [116.481573, 39.997997],
  [116.481863, 39.997846],
  [116.482072, 39.997718],
  [116.482362, 39.997718],
  [116.483633, 39.998935],
  [116.48367, 39.998968],
  [116.484648, 39.999861]
])
const initMap = () => {
  // 初始化地图
  console.log(AMap)

  map.value = new AMap.Map('container', {
    resizeEnable: true, // 窗口大小调整
    viewMode: '2D',
    center: [116.397428, 39.90923],
    zoom: 17
  })

  // 添加maker  MassMarks为海量marker
  marker.value = new AMap.Marker({
    map: map.value,
    position: [116.478935, 39.997761],
    icon: 'https://webapi.amap.com/images/car.png',
    offset: new AMap.Pixel(-26, -15), // 调整图片偏移
    autoRotation: true, // 自动旋转
    angle: -90 // 图片旋转角度
  })
  marker.value.setLabel({
    content: '内部',
    offset: new AMap.Pixel(-60, 0)
  })
  const point = marker.value.getPosition() // 获取marker位置
  console.log(point)

  // //  圆形
  Circle.value = new AMap.Circle({
    map: map.value,
    center: [116.478935, 39.997761], // 站点坐标，定位圆点位置
    radius: Number(200), // Circle半径，我取得是父组件传过来的值
    strokeColor: 'white', // 边框颜色
    strokeWeight: 2, // 边框宽度
    strokeOpacity: 0.5, // 边框透明度
    fillColor: 'rgba(0,0,128,64)', // 圆点填充颜色
    fillOpacity: 0.2 // 圆点填充透明度
  })
  const CirclePath = Circle.value.getPath()
  console.log(CirclePath) // 获取圆形路径

  const isPointInRing = AMap.GeometryUtil.isPointInRing(point, CirclePath) // 判断是位置否在多边形内
  console.log(isPointInRing)

  // // 用多边形的几个顶点画多边形，数据和上面的那个是一样的
  const polyPath = [
    [116.480107, 39.999961],
    [116.475601, 39.997413],
    [116.480687, 39.994059],
    [116.48412, 39.997002],
    [116.480107, 39.999961]
  ]
  const polyline = new AMap.Polyline({
    path: polyPath,
    strokeColor: 'red'
  })
  map.value.add(polyline) //  添加 多边形路径

  // // 绘制还未经过的轨迹路线
  const routeLine = new AMap.Polyline({
    map: map.value,
    path: lineArr.value,
    showDir: true,
    strokeColor: '#28F', // 线颜色
    // strokeOpacity: 1,     //线透明度
    strokeWeight: 6, // 线宽
    // strokeStyle: "solid"  //线样式
    lineJoin: 'round' // 折线拐点的绘制样式
  })

  // // 绘制路过了的轨迹
  const passedPolyline = new AMap.Polyline({
    map: map.value,
    // path: lineArr,
    strokeColor: '#AF5', // 线颜色
    // strokeOpacity: 1,     //线透明度
    strokeWeight: 6 // 线宽
    // strokeStyle: "solid"  //线样式
  })

  // marker监听 移动时 事件
  marker.value.on('moving', function (e: { passedPath: any }) {
    passedPolyline.setPath(e.passedPath) // 设置 绘制路过了的轨迹 的路径为 marker的路径
    compute(CirclePath)
    compute(polyPath)
  })

  // // marker 监听 点击时 事件
  marker.value.on('click', function (e: { passedPath: any }) {
    console.log('点击')
    console.log(e.lnglat)
    openInfo(e.lnglat)
  })

  map.value.setFitView() // 视口 缩放地图到适合的视线级别
}

// 在指定位置打开信息窗体
function openInfo(lnglat) {
  // 构建信息窗体中显示的内容
  const info = []
  const temp = `<div style="font-size:12px;color:#222;width:160px;padding:10px">
                    <div style="line-height:24px;">
                        <label style="margin-right:10px;">车牌号</label> 
                        <span style="color:#909399">川A88888</spam>  
                    </div>
                    <div style="line-height:24px">
                        <label style="margin-right:10px;">救援司机</label> 
                        <span style="color:#909399">交警大队</spam>  
                    </div>
                    <div style="line-height:24px">
                        <label style="margin-right:10px;">服务商</label> 
                        <span style="color:#909399">奔驰</spam>  
                    </div>
                    <div style="line-height:24px">
                        <label style="margin-right:10px;">手机号</label> 
                        <span style="color:#909399">1333333333</spam>  
                    </div>
                    <div style="line-height:24px">
                        <label style="margin-right:10px;">车辆状态</label> 
                        <span style="color:'#00C80C'">进行中</spam>  
                    </div>
                    <div style="line-height:24px">
                        <label style="margin-right:10px;">车辆类型</label> 
                        <span style="color:#909399">大货车</spam>  
                    </div>
                </div>`
  info.push(temp)

  const infoWindow = new AMap.InfoWindow({
    content: info.join('') // 使用默认信息窗体框样式，显示信息内容
  })

  infoWindow.open(map.value, lnglat)
}

// 判断是位置否在多边形内
function compute(path: Array<any>) {
  const point = marker.value.getPosition()
  const isPointInRing = AMap.GeometryUtil.isPointInRing(point, path) // 判断是位置否在多边形内
  marker.value.setLabel({
    content: isPointInRing ? '内部' : '外部',
    offset: new AMap.Pixel(-60, 0)
  })
}

// 开始绘制动画
const startAnimation = () => {
  // marker.value.moveTo([116.484648, 39.999861], 200);
  // https://lbs.amap.com/api/javascript-api/reference/overlay
  // 以指定的速度，点标记沿指定的路径移动。参数path为轨迹路径的经纬度对象的数组；speed为指定速度，单位：千米/小时，不可为0；
  marker.value.moveAlong(lineArr.value, 1500)
}
const pauseAnimation = () => {
  marker.value.pauseMove()
}
const resumeAnimation = () => {
  marker.value.resumeMove()
}
const stopAnimation = () => {
  marker.value.stopMove()
}
</script>
<style lang="scss" scoped>
        // @import url('https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css');

        #container {
            width: 800px;
            height: 500px;
        }

        .input-card .btn {
            width: 9rem;
            margin-right: 1.2rem;
        }

        .input-card .btn:last-child {
            margin-right: 0;
        }

        .btn {
            display: inline-block;
            padding: 0.25rem 0.5rem;
            font-weight: 400;
            line-height: 1.5;
            color: #25a5f7;
            text-align: center;
            white-space: nowrap;
            vertical-align: middle;
            cursor: pointer;
            user-select: none;
            background-color: transparent;
            background-image: none;
            border: 1px solid transparent;
            border-color: #25a5f7;
            border-radius: 1rem;
            transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
                border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
            appearance: button;
        }

        .input-item {
            position: relative;
            display: flexbox;
            display: flex;
            flex-wrap: wrap;
            -ms-flex-align: center;
            align-items: center;
            width: 100%;
            height: 3rem;
        }

        .input-card {
            position: fixed;
            right: 1rem;
            bottom: 1rem;
            display: flex;
            flex: 1 1 auto;
            flex-direction: column;
            width: 22rem;
            min-width: 0;
            padding: 0.75rem 1.25rem;
            word-wrap: break-word;
            background-color: #fff;
            background-clip: border-box;
            border-width: 0;
            border-radius: 0.4rem;
            box-shadow: 0 2px 6px 0 rgb(114 124 245 / 50%);
        }
</style>
